home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk.zip / MATHERR.C < prev    next >
C/C++ Source or Header  |  1991-04-07  |  2KB  |  107 lines

  1.  
  2. /********************************************
  3. matherr.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the Awk programming language as defined in
  8. Aho, Kernighan and Weinberger, The AWK Programming Language,
  9. Addison-Wesley, 1988.
  10.  
  11. See the accompaning file, LIMITATIONS, for restrictions
  12. regarding modification and redistribution of this
  13. program in source or binary form.
  14. ********************************************/
  15.  
  16. /*$Log:    matherr.c,v $
  17.  * Revision 2.1  91/04/08  08:23:31  brennan
  18.  * VERSION 0.97
  19.  * 
  20. */
  21.  
  22. #include  "mawk.h"
  23. #include  <math.h>
  24.  
  25. #if   FPE_TRAPS
  26. #include <signal.h>
  27.  
  28. /* machine dependent changes might be needed here */
  29.  
  30. static void  fpe_catch( signal, why)
  31.   int signal, why ;
  32. {
  33.   switch(why)
  34.   {
  35.     case FPE_ZERODIVIDE :
  36.        rt_error("division by zero") ;
  37.  
  38.     case FPE_OVERFLOW  :
  39.        rt_error("floating point overflow") ;
  40.  
  41.     default :
  42.       rt_error("floating point exception") ;
  43.   }
  44. }
  45.  
  46. void   fpe_init()
  47. { (void) signal(SIGFPE, fpe_catch) ; }
  48.  
  49. #else
  50.  
  51. void  fpe_init()
  52. {
  53.   TURNOFF_FPE_TRAPS() ;
  54. }
  55. #endif
  56.  
  57. #if  HAVE_MATHERR
  58.  
  59. #if  ! FPE_TRAPS 
  60.  
  61. /* If we are not trapping math errors, we will shutup the library calls
  62. */
  63.  
  64. int  matherr( e )
  65.   struct exception *e ;
  66. { return 1 ; } 
  67.  
  68. #else   /* print error message and exit */
  69.  
  70. int matherr( e )
  71.   struct exception  *e ;
  72. { char *error ;
  73.  
  74.   switch( e->type )
  75.   {
  76.     case  DOMAIN :
  77.     case  SING :
  78.             error = "domain error" ;
  79.             break ;
  80.  
  81.     case  OVERFLOW :
  82.             error = "overflow" ;
  83.             break ;
  84.  
  85.     case  TLOSS :
  86.     case  PLOSS :
  87.             error = "loss of significance" ;
  88.             break ;
  89.  
  90.     case  UNDERFLOW :
  91.             e->retval = 0.0 ;
  92.             return  1 ;  /* ignore it */
  93.   }
  94.  
  95.   if ( strcmp(e->name, "atan2") == 0 )
  96.       rt_error("atan2(%g,%g) : %s" ,
  97.          e->arg1, e->arg2, error ) ;
  98.   else
  99.       rt_error("%s(%g) : %s" , e->name, e->arg1, error) ;
  100.  
  101.   /* won't get here */
  102.   return 0 ;
  103. }
  104. #endif   /* FPE_TRAPS */
  105.  
  106. #endif   /*  HAVE_MATHERR */
  107.